//
//  ToViewController.m
//  6-6.2016
//
//  Created by mac on 16-6-6.
//  Copyright (c) 2016年 hf. All rights reserved.
//

#import "ToViewController.h"

@interface ToViewController ()

@end

@implementation ToViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"第二个View";
    self.view.backgroundColor = [UIColor brownColor];
    UIButton *bn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bn setTitle:@"抹茶" forState:UIControlStateNormal];
    [bn setTitle:@"买单" forState:UIControlStateHighlighted];
    [bn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal ];
    bn.layer.borderWidth = 1;
    bn.layer.cornerRadius = 3;
    bn.frame = CGRectMake(0, 0, 65, 44);
    bn.center = CGPointMake(self.view.frame.size.width/7, 100);
    [bn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchDown ];
    [bn addTarget:self action:@selector(onClick2:) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:bn];
    
    UILabel *l = [[UILabel alloc]initWithFrame:CGRectMake(15, 150, 100, 44)];
    l.text = @"余茉";
    l.tag = 100;
    UILabel *n = [[UILabel alloc]initWithFrame:CGRectMake(15, 190, 100, 44)];
    n.text = @"锦鱼的记忆";
    
    [self.view addSubview:n];
    [self.view addSubview:l];
    
    UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(260, 155, 100, 44)];
    [self.view addSubview:sw];
    UISwitch *st = [[UISwitch alloc]initWithFrame:CGRectMake(260, 195, 100, 44)];
    [self.view addSubview:st];
    
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"左对齐",@"居中",@"右对齐"]];
    [seg sizeToFit];
    seg.center = CGPointMake(160, 290);
    [seg addTarget:self action:@selector(onSeg:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:seg];
    
}

- (void)onSeg:(UISegmentedControl *)seg
{
    UILabel *l = (UILabel *)[self.view viewWithTag:100];
    [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        if (seg.selectedSegmentIndex == 0)
        {
            l.center = CGPointMake(16 + l.frame.size.width/2, l.center.y);
        }
        else if(seg.selectedSegmentIndex == 1)
        {
            l.center = CGPointMake(self.view.frame.size.width/2, l.center.y);
        }
        else
        {
            l.center = CGPointMake(self.view.frame.size.width - (16 + l.frame.size.width/2), l.center.y);
        }
    } completion:nil];
}

- (void)onSwitch:(UISwitch *)sw
{
    UILabel *l = (UILabel *)[self.view viewWithTag:100];
    if (sw.isOn)
    {
        l.layer.borderWidth = 1;
    }
    else
    {
        l.layer.borderWidth = 0;
    }
}

- (void)onClick:(UIButton *)sender
{
    sender.backgroundColor = [UIColor redColor];
}

- (void)onClick2:(UIButton *)sender
{
    sender.backgroundColor = [UIColor greenColor];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
